#include "main.h"


#define LBI_MAX 999 	         	// 油烟最大数值 

#if FAN_SPPED_MAX7
	#define FAN_CUR_LEVEL_NUM 		7 		// 档位数量
#else
	#define FAN_CUR_LEVEL_NUM 		3 		// 档位数量
#endif  

#define LEVEL_LBI_VALUE (LBI_MAX/FAN_CUR_LEVEL_NUM)// 每档间隔值

#define FILTER_NUM   16

LBI_CB lbiCB;

//油烟LBI指数 平滑滤波 返回经平滑滤波后的油油烟指数
void LBI_DataFilter(void)
{
	static u16 buf[FILTER_NUM] = {0};
	u32 tempBuf = 0;
	u8 i = 0;

	for(i = 0;i < FILTER_NUM;i++)
	{
		buf[i] = buf[i+1];
	}
	buf[FILTER_NUM-1] = m_Smoke_data.u16;

	
	for (i = 0;i < FILTER_NUM;i++)
	{
		tempBuf += buf[i];
	}
	m_Smoke_data_LBI.u16 = tempBuf/FILTER_NUM;/// 获取当前移动均值作为档位选择的依据
}

void Task_Oil_Event(void)
{
	LBI_DataFilter();
}


//风机档位切换处理
void LBI_FanLevelDeal_1sRoutine(void)
{
static	unsigned char Cnt =0;
	
#if FAN_SPPED_MAX7	
	if(m_Smoke_data_LBI.u16 >= 994)
		m_Smoke_data_LBI.u16 = 993;
#else
if(m_Smoke_data_LBI.u16 >= 999)
		m_Smoke_data_LBI.u16 = 998;
#endif	
	
	if(Power_Down_data_New.Control2.bits.Auto_Fan)
	{
		lbiCB.curspeed = (eFAN_speed)((m_Smoke_data_LBI.u16/LEVEL_LBI_VALUE)+1);//+1; 当前油烟值对应的档位
	}
	else
	{
		Cnt =0;
		lbiCB.curspeed =  tran_Fan_Speed((eFAN_LEVEL)Power_Down_data_New.Fan_Attribute1.bits.Fan_Level);
		lbiCB.workLevel =Get_Fan_Level(lbiCB.curspeed);
	}
	

	if( lbiCB.curspeed <tran_Fan_Speed((eFAN_LEVEL)Power_Down_data_New.Fan_Attribute2.bits.Fan_MinLevel))
	{
		
		lbiCB.curspeed =tran_Fan_Speed((eFAN_LEVEL)Power_Down_data_New.Fan_Attribute2.bits.Fan_MinLevel);
	}


	if(lbiCB.curspeed > tran_Fan_Speed((eFAN_LEVEL)Power_Down_data_New.Fan_Attribute1.bits.Fan_Level) )    // 若当前油烟浓度对应的档位大于当前风机档位，则立即切换至最新的档位
	{
			Cnt =0;
#if FAN_SPPED_MAX7
			lbiCB.workLevel =Get_Fan_Level(lbiCB.curspeed);
#else

		lbiCB.workLevel =Get_Fan_Level(lbiCB.curspeed);
#endif	
	}
	else if(lbiCB.curspeed < tran_Fan_Speed((eFAN_LEVEL)Power_Down_data_New.Fan_Attribute1.bits.Fan_Level))	// 若当前油烟浓度对应的档位小于当前风机档位，则进行下一判断 降档逻辑
	{
		lbiCB.levelSwichVlaue = LEVEL_LBI_VALUE*lbiCB.curspeed -20;  	// 计算档位降档时，对应的回差油烟浓值
		if (m_Smoke_data_LBI.u16 < lbiCB.levelSwichVlaue)		// 若当前油烟浓度满足回差，则开始降档逻辑
		{
			if(Cnt<10)
			{
				Cnt++;
				if(Cnt ==10)
				{
					Cnt =0;
					lbiCB.workLevel =Get_Fan_Level(lbiCB.curspeed);
					//lbiCB.workLevel =(eFAN_LEVEL)(Power_Down_data_New.Fan_Attribute1.bits.Fan_Level-1);
				}
			}
		}	
	}
	else
	{
		Cnt =0;
	}
}

////100ms执行一次 根据档位信息,计算对应的烟机控制电流。
//void LBI_FanCurrentDeal(void)
//{
//	static u8 tempCurrent11 = 0;  // 烟机电流中间过渡变量
//	static u8 tempCurrent12 = 0;
//	static u16 count = 0;
//	LBI_FanLevelDeal(); // 获取当前油烟浓度对应档位
//	
//	if(!lbiCB.levelDown)
//	{
//		count = 0;
//		tempCurrent11 = levelCurrent[lbiCB.workLevel];	// 风机驱动电流计算	
//		m_fan_speed = lbiCB.workLevel;
//	}
//	else if(lbiCB.levelDown)
//	{
//		tempCurrent12 = levelCurrent[lbiCB.workLevel];	// 降档目标档位对应电流
//		if(tempCurrent11 > tempCurrent12)
//		{
//			if(f_ctrlmark9.bits.m_fan_mode == MODE_ID_CURVE)
//			{
//				if(++count >= (10000/LBI_TIME)) // 10s 降低当前浓度档位
//				{
//					count = 0;
//					tempCurrent11 = levelCurrent[lbiCB.workLevel];
//				}
//				else;
//			}
//			else
//			{
//				if(++count >= (1000/LBI_TIME)) // 1s 降低0.01的电流
//				{
//					count = 0;
//					tempCurrent11 -= 1;
//				}
//				else;
//			}
//		}
//		else
//			lbiCB.levelDown = false;		
//	}
//	else;
//	
////	m_fan_speed = lbiCB.workLevel;
//	if(m_fan_speed > c_set_fan_three)
//		m_fan_speed = c_set_fan_baochao;
//		
//	m_set_motoSpeed.Full = tempCurrent11;
//	
//	if( ( (m_box_mode == c_oven_RFSK_mode) || (m_box_mode == c_oven_SXJK_mode) || (m_box_mode == c_oven_ZQNK_mode) || (m_box_mode == c_oven_KQSZ_mode) )		//烤箱工作时风机强制钳位2档
//		&& ( (m_flash_cnt == c_flash_off) || (m_flash_cnt >= c_flash_set_fr_timer_off_sta) )
//		&& (!m_box.bits.b_timer_on_sta) /*&& (!m_box.bits.b_pause_sta)*/ )
//	{
//		if(m_fan_speed < c_set_fan_two)
//		{
//			m_fan_speed = lbiCB.workLevel = lbiCB.curLevel = c_set_fan_two;
//			m_set_motoSpeed.Full = levelCurrent[lbiCB.workLevel];
//		}
//	}
//}
